<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 数据库查询监视器
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Db_Monitor implements Countable, IteratorAggregate
{

    private $_count = 0;
    private $_totalSecs = 0;
    private $_allQuery = array();

    /**
     * 更新监视器
     *
     * @param  string  $sql   数据库查询语句
     * @param  float   $secs  查询消耗的时间(秒)
     */
    public function update($sql, $secs)
    {
        $index = $this->_count++;
        $this->_allQuery[$index] = new StdClass();
        $this->_allQuery[$index]->secs = $secs;
        $this->_allQuery[$index]->sql = $sql;
        $this->_totalSecs += $secs;
    }

    /**
     * 获取监视器数据
     *
     * @param  string  $name  数据名称
     * @return mixed
     */
    public function get($name)
    {
        return isset($this->{"_$name"}) ? $this->{"_$name"} : null;
    }

    /**
     * 使用魔术方法获取监视器数据
     *
     * @param  string  $name  数据名称
     * @return mixed
     */
    public function __get($name)
    {
        return $this->get($name);
    }

    /**
     * 返回给 foreach 调用的接口
     *
     * @return ArrayObject
     */
    public function getIterator()
    {
        return new ArrayObject($this->_allQuery);
    }

    /**
     * 返回 count 函数获得的值
     *
     * @return integer
     */
    public function count()
    {
        return $this->_count;
    }

}